﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using WebApi2b.Data;
using WebApi2b.Data.Entity;
using WebApi2b.Domain.Impementation;
using WebApi2b.Domain.Interface;

namespace WebApi2b.Utils
{
    public class DbInitializerHelper
    {
        public static void Seed(IServiceProvider serviceProvider) 
        {
            using (var scope=serviceProvider.CreateScope()) 
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin10086DbContext)) as Admin10086DbContext;

                var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EfResPository<Users>;
                var _rolesRespository = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EfResPository<Roles>;

                db.Database.EnsureCreated();

                var hasuser = db.Users.Any();

                if (!hasuser)
                {
                    var role = new Roles
                    {
                        RoleName = "管理员",
                        Description = "这是这是一个管理员"
                    };
                    _rolesRespository.Insert(role);
                    //db.Roles.Add(role);
                    //db.SaveChanges();

                    _userRespository.InsertBulk(new Users[]
                    {
                        new Users
                        {
                            UserName="大壮",
                            Passsword="123456",
                            RolesId=role.Id
                        },
                        new Users
                        {
                            UserName="盖伦",
                            Passsword="123456",
                            RolesId=role.Id
                        }
                    });

                    //db.SaveChanges();
                }
            }
        }
    }
}
